package 课堂;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author song
 * @date 2017/11/10 14:33
 */
public class 蛮力算法简化 {
    public static void main(String[] args) {
        int[] a = new int[10];
        for (int p = 0; p < a.length; p++) {
            a[p] = (int) (Math.random() * 10);
        }
        System.out.println(maxSubSequenceSum(a));
        System.out.println(seqStart + "------" + seqEnd);
    }

    static int seqStart = 2;
    static int seqEnd = 5;

    public static int maxSubSequenceSum(int[] a) {
        int maxSum = 0;
        int thisSum = 0;
        for (int i = 0, j = 0; j < a.length; j++) {
            thisSum += a[j];
            if (thisSum > maxSum) {
                maxSum = thisSum;
                seqStart = i;
                seqStart = j;
            } else if (thisSum < 0) {
                i = j + 1;
                thisSum = 0;
            }
        }
        return maxSum;
    }
}
